package com.haiyou.dc.aggregate.linklist;

public abstract class MyAbstractList<E> implements MyList<E> {

    protected int size;

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean contains(E element) {
        return indexOf(element) >= 0;
    }

    protected void rangeCheck(int index) {
        if (index >= size) {
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
        }
    }

    protected void rangeCheckForAdd(int index) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
        }
    }

    @Override
    public int add(E element) {
        return add(size, element);
    }

    protected String outOfBoundsMsg(int index) {
        return "超出索引:[index=" + index + ",size=" + size + "]";
    }

}
